Imports Microsoft.VisualBasic
Imports System

Public Class Calcs

    Private Shared seed As Integer = 0
    Public Shared Function CalcProbability2(ByVal player As Base) As Boolean


        Dim injuryRisk As Double = CType(player.Setup, PlayerSetup).ProbabiltyOfInjury() 'as decimal 0.05 = 5%

        Dim percent As Integer = CInt(injuryRisk * 100)
        Dim gen As Integer = GenerateNumberRandom(percent)
        If (gen <= percent) Then
            Return True
        Else
            Return False
        End If

    End Function

    Public Shared Function GenerateNumberRandom(ByVal arg1 As Integer, Optional ByVal max As Integer = 100) As Integer
        Dim _random As New Random(New Random(New Random(GetSeed(CInt(GetSeed() / 3))).Next(Now.Millisecond)).Next(Now.Millisecond))
        Dim num As Integer = _random.Next(1, max + 1)
        Return num
    End Function

    Private Shared Function GetSeed(Optional ByVal arg1 As Integer = 1) As Integer

        'Dim cls As New clsRandom
        'seed = cls.RandomIntegers(1, 1000000000, 1)(0)

        Dim cls As New TrueRandom2
        seed = cls.GetNext()
        'If seed > 65355 Then
        '    seed = 0
        'ElseIf seed Mod 2 = 0 Then
        '    seed /= arg1
        'Else
        '    seed *= arg1
        'End If

        'seed += 1

        Return seed
    End Function



    Public Shared Function CalcInjuryType(ByVal simul As Base, ByVal player As Base, ByVal injuryCol As Collection) As Base

        Dim injuries As Hashtable = CType(simul.Setup, SimulationSetup).InjuriesMap

        Dim playerSetup As PlayerSetup = player.Setup
        Dim probInjury As Double = playerSetup.ProbabiltyOfInjury

        Dim age As Integer = playerSetup.Age
        Dim ageRange As String = playerSetup.GetAgeRange
        Dim total As Double = 0

        Dim boundaries As New Collection
        Dim percent As Integer

        Dim ageRangeMap As Hashtable
        For Each id As Integer In injuries.Keys
            ageRangeMap = injuries.Item(id)
            percent = ageRangeMap.Item(ageRange)
            total += percent

            boundaries.Add(New Bounds(GetInjuryByID(injuryCol, id), total))
        Next

        '      50, 100 = if injured (p(I) = 20%)
        'total =  ^    +                          + 
        '100 = 20
        ' x  = 80?
        'total += total
        total += (total / probInjury) * (1.0 - probInjury)

        boundaries.Add(New Bounds(New Base(-1, "NONE", "Dummy"), total)) 'no injury

        Dim gen As Integer = GenerateNumberRandom(total, total)

        '10,10,10,10  40 4 injuries, all 10%
        '10,20,30,40 80

        For Each bound As Bounds In boundaries
            If gen <= bound._val Then
                If bound._base.ID = -1 Then
                    Return Nothing
                Else
                    Return bound._base
                End If

            End If
        Next

        Return Nothing

    End Function

    Private Shared Function GetInjuryByID(ByVal injuriesCol As Collection, ByVal id As Integer) As Base

        For Each injury As Base In injuriesCol

            If (injury.ID = id) Then
                Return injury
            End If

        Next
        Return Nothing
    End Function

    Public Class Bounds
        Implements IComparable  'IMPORTANT
        Public _base As Base
        Public _val As Double

        Public Sub New(ByVal base As Base, ByVal val As Double)
            _base = base
            _val = val
        End Sub



        Public Function CompareTo(ByVal obj As Object) As Integer Implements IComparable.CompareTo
            If Me._val < _val Then
                Return -1
            End If

        End Function

    End Class


    Public Shared Function CalcRecoveryPeriod(ByVal player As Base, ByVal injury As Base) As Integer
        Dim pSetup As PlayerSetup = player.Setup
        Dim iSetup As InjurySetup = injury.Setup

        Dim ageRange As String = pSetup.GetAgeRange

        Dim injuryWeeksOut As Integer = iSetup.RecoveryWeeks '4
        Dim recoveryPercent As Integer = iSetup.RecoveryAgeMap.Item(ageRange) '30

        '0.30 * 4 = 
        Dim newWeeksOut As Double = injuryWeeksOut * (recoveryPercent / 100.0)

        Return CInt(Math.Round(newWeeksOut))
    End Function

    Public Shared Function TrainingBoostBasedOnAgeRange(ByVal player As Base, ByVal training As Base) As Double

        Dim range As String = CType(player.Setup, PlayerSetup).GetAgeRange
        Dim boost As Double = CType(training.Setup, TrainingSetup).PointsForFUllTraining

        Dim percent As Integer = CType(training.Setup, TrainingSetup).TrainingAgeMap.Item(range)

        Dim actualBoost As Double = (percent / 100.0) * boost

        Return actualBoost
    End Function

End Class
